package Array;

/**
 * LC209，滑动窗口法，本质上还是双指针，但是因为组成的窗口在一直滑动
 *
 */
public class MinSubArrayLength {
    public static void main(String[] args) {
        int[] sums={2,3,1,2,4,3};
        int[] sums2={1,4,4};
        MinSubArrayLength solution = new MinSubArrayLength();
        System.out.println(solution.minSubArrayLen(4,sums2));
        //System.out.println(solution.minSubArrayLen(7,sums));

    }

    public int minSubArrayLen(int s, int[] nums) {
        int left=0;
        int sum=0;
        int result=Integer.MAX_VALUE;
        for(int right=0;right<nums.length;right++){
            sum += nums[right];
            while(sum>=s){
              result=Math.min(result,right-left+1);
              sum -= nums[left];
              left++;
            }
        }
        return result == Integer.MAX_VALUE ? 0 : result;

    }


}
